package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.User;
import com.example.wechatdaka.service.UserService;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result queryAll(){

        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.orderByAsc("lxRoleId");
        List<User> userList = userService.list(userQueryWrapper);


        if (userList!=null){
            System.out.println("userList:"+userList.toString());
            return new Result(Result.SUCCESS,userList,"success");
        }
        return new Result(Result.ERROR,null,"");
    }

    @RequestMapping(value = "/indexByPage",method = RequestMethod.GET)
    public JsonResult queryAllByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.orderByAsc("lxRoleId").orderByAsc("lxUserNumber");
        Page<User> page = new Page<>(currentPage, pagesize);
        Page<User> userPage = userService.page(page,userQueryWrapper);
        List<User> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @RequestMapping(value = "/queryByRolePage",method = RequestMethod.GET)
    public JsonResult queryByRolePage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String id) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("lxRoleId",id).orderByAsc("lxUserNumber");
        Page<User> page = new Page<>(currentPage, pagesize);
        Page<User> userPage = userService.page(page,userQueryWrapper);
        List<User> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Result insertUser(@RequestBody User user){
        System.out.println("user:"+user.toString());
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("lxUserNumber",user.getLxUserNumber());
        User one = userService.getOne(userQueryWrapper);
        if (one!=null){
            return new Result(Result.ERROR,null,"该学号已存在");
        }
        boolean save = userService.save(user);
        if (save){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateUser(@RequestBody User user){
        System.out.println("user:"+user.toString());
        boolean save = userService.updateById(user);
        if (save){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result dedleteUser(@RequestParam(value = "id") String id){
        System.out.println("user:"+id);
        boolean b = userService.removeById(id);
        if (b){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
    @RequestMapping(value = "/findByKeyword",method = RequestMethod.POST)
    public Result findUserByKeyword(String keyword){
        System.out.println("keyword:"+keyword);
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.like("lxUsername", keyword);
        List<User> userList = userService.list(userQueryWrapper);
        System.out.println("user:"+userList.toString());

        if (userList!=null){
            return new Result(Result.SUCCESS,userList,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
    @RequestMapping(value = "/queryByNumber",method = RequestMethod.POST)
    public Result queryByNumber(@RequestBody String lxUserNumber) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("lxUserNumber",lxUserNumber);
        User one = userService.getOne(userQueryWrapper);
        return new Result(Result.SUCCESS,one,"success");
    }
    @RequestMapping(value = "/queryById",method = RequestMethod.POST)
    public Result queryById(@RequestBody String id) {
        User one = userService.getById(id);
        return new Result(Result.SUCCESS,one,"success");
    }

}

